#!/bin/sh

PREFIX=@prefix@

# stop any existing instances
$PREFIX/bin/sqlr-stop
sleep 2

# really kill them
kill `ps aux | grep sqlr-connection | grep -v grep | cut -c10-15` 2> /dev/null
kill `ps aux | grep sqlr-scaler | grep -v grep | cut -c10-15` 2> /dev/null
kill `ps aux | grep sqlr-listener | grep -v grep | cut -c10-15` 2> /dev/null
sleep 2

# for each database/configuration...
for DB in  @TESTDBS@
do

	# for tls, krb, and extensions tests, verify that we support oracle
	if ( test "$DB" = "tls" -o "$DB" = "krb" -o "$DB" = "extensions" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_oracle.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi

	# for other tests, verify that we support the database
	else
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_$DB.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# for router tests, also verify that we support mysql
	if ( test "$DB" = "router" )
	then
		if ( test -z "`ls $PREFIX/lib*/sqlrelay/sqlrconnection_mysql.so 2> /dev/null`" )
		then
			echo "skipping $DB..."
			echo
			echo "================================================================================"
			echo
			continue
		fi
	fi

	# testing...
	echo "testing $DB (from `hostname`)..."

	# for the router test, start the master/slave instances
	if ( test "$DB" = "router" )
	then
		$PREFIX/bin/sqlr-start -id routermaster -backtrace `pwd`
		sleep 2

		$PREFIX/bin/sqlr-start -id routerslave -backtrace `pwd`
		sleep 2
	fi

	# start the instance
	$PREFIX/bin/sqlr-start -id ${DB}test -backtrace `pwd`
	sleep 2

	# make sure that the instance is up
	PING=""
	for attempts in 1 2 3 4 5 6 7 8 9 10
	do
		echo
		echo "pinging $DB..."
		if ( test "$DB" = "tls" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -tls -tlscert $PREFIX/etc/client.pem -tlsca $PREFIX/etc/ca.pem -tlsvalidate ca -command ping 2>&1`
		elif ( test "$DB" = "extensions" )
		then
			PING=`$PREFIX/bin/sqlrsh -host localhost -user test -password test -command ping 2>&1`
		else
			PING=`$PREFIX/bin/sqlrsh -id ${DB}test -command ping 2>&1`
		fi

		# (this collapses whitespace)
		PING=`echo $PING`

		echo $PING

		if ( test "$PING" = "0: Couldn't connect to the listener." )
		then
			if ( test "$DB" = "db2" )
			then
				sleep 5
			else
				sleep 2
			fi
		else
			break
		fi
	done

	# run the tests
	if ( test "$PING" = "The database is up." )
	then

		# kinit for krb tests
		if ( test "$DB" = "krb" )
		then
			kdestroy
			kinit
		fi

		echo
		echo "success..."
		echo
		./test.sh $DB
	else
		echo
		echo "failed to start ${DB}test"
		echo
		echo "hit enter to continue or ctrl-c to stop..."
		if ( test "$DB" = "krb" )
		then
			read -t 20 PROMPT 2> /dev/null
			# (ubuntu's dash doesn't support read -t)
			if ( test "$?" != "0" )
			then
				sleep 20
			fi
		else
			read PROMPT
		fi
	fi

	# shut down the instance(s)
	if ( test "$DB" = "router" )
	then
		sleep 2
		$PREFIX/bin/sqlr-stop -id routermaster
		sleep 2
		$PREFIX/bin/sqlr-stop -id routerslave
	fi
	sleep 2
	$PREFIX/bin/sqlr-stop -id ${DB}test
	sleep 2

	echo
	echo "================================================================================"
	echo
done
